package com.alibaba.otter.manager.web.service;

import com.alibaba.otter.manager.biz.config.alarm.AlarmRuleService;
import com.alibaba.otter.shared.common.model.config.alarm.AlarmRule;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlarmRuleAggService {


    private final AlarmRuleService alarmRuleService;

    public AlarmRuleAggService(AlarmRuleService alarmRuleService) {
        this.alarmRuleService = alarmRuleService;
    }

    public void statusByPipelineId(Long pipelineId, String status, String pauseTime) {

        List<AlarmRule> alarmRules = alarmRuleService.getAlarmRules(pipelineId);
        for (AlarmRule alarmRule : alarmRules) {
            statusByRule(status, alarmRule.getId(), pauseTime);
        }
    }


    public void statusByRule(String alarmRuleIds, String status, String pauseTime) {

        String[] a = alarmRuleIds.split(",");
        for (String alarmRuleId : a) {
            statusByRule(status, Long.valueOf(alarmRuleId), pauseTime);
        }
    }

    public void statusByRule(String status, Long alarmRuleId, String pauseTime) {
        if (status.equals("enable")) {
            alarmRuleService.enableMonitor(alarmRuleId);
        } else if (status.equals("disable")) {
            if (pauseTime != null) {
                alarmRuleService.disableMonitor(alarmRuleId, pauseTime);
            } else {
                alarmRuleService.disableMonitor(alarmRuleId);
            }
        }
    }
}
